<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/19 0019
 * Time: 14:41
 */

namespace app\sisome\controller;


use think\Db;
use think\Request;

class Tousu extends Common
{

    /**
     * @name 投诉列表
     * @author zay
     * @Date 2020/3/19 0019 14:49
     * @return \think\response\View
     */
    public function get_list(Request $request)
    {
        //KM++投诉列表
        $type = $request->param('type', 0);
        $list = DB('tousu')->where(['is_del' => 0]);
        if ($type == 0) {
            $list->whereIn('zt', [0, 2]);
        } else {
            $list->whereIn('zt', [1, 3]);
        }
        $list = $list->order('id desc')->paginate(20);
        $this->assign('lists', $list);
        return view();
    }
    /**
     * @name 投诉处理
     * @author zay
     * @Date 2020/3/19 0019 15:25
     */

    public function tscl(Request $request)
    {
        DB::startTrans();
        $is_success = 0;
        $msg = '';
        try {
            $id = $request->param('id', 0);
            $type = $request->param('type');//1投诉成功 撤销单子 2投诉失败 订单完成
            if (empty($id)) {
                throw new \Exception('数据错误');
            }
            if (!in_array($type, [1, 3])) {
                throw new \Exception('数据错误');
            }
            $tousu = DB('tousu')->where(array('id' => $id, 'is_del' => 0))->find();
            if ($tousu['zt'] == 1) {
                throw new \Exception('投诉已处理');
            }
            $res = DB('tousu')->where(array('id' => $id))->update(array('zt' => $type, 'deal_time' => time()));
            if (!$res) {
                throw new \Exception('投诉处理失败');
            }
            $oob = Db('ppdd_jiaoyi')->where(['id' => $tousu['pid']])->find();
            if (empty($oob)) {
                throw new \Exception('交易记录不存在');
            }
            if ($type == 1) {
                /*$order = Db('ppdd_jiaoyi')->where(['id' => $oob['pid']])->find();
                if ($order['zt'] == 5 && $order['buy_user'] == $oob['buser']) {//买家被投诉
                    $setting = setting();
                    $userinfo = userinfobyid($order['buy_id']);
                    //给买家扣币处罚
                    $res = Db('user')->where(['id' => $order['buy_id']])->update(['idc' => $userinfo['idc'] - $setting['buy_fee'], 'update_time' => time()]);
                    if (!$res) {
                        throw new \Exception('买家处罚扣币失败');
                    }
                    //
                    $log_insert = [
                        'uid' => $order['buy_id'],
                        'uname' => $order['buy_user'],
                        'type' => 9,
                        'mqian' => $userinfo['idc'],
                        'money' => -$setting['buy_fee'],
                        'mhou' => $userinfo['idc'] - $setting['buy_fee'],
                        'note' => '买家被投诉处罚',
                        'order_id' => $order['id'],
                        'order_sn' => $order['g_order_sn'],
                        'time' => time(),
                    ];
                    $res = Db('logocoin')->insert($log_insert);
                    if (!$res) {
                        throw new \Exception('币使用记录添加失败');
                    }
                }*/
                $res = Db('ppdd_jiaoyi')->where(['id' => $oob['id'], 'zt' => 5])->update(['zt' => 3, 'update_time' => time()]);
                if (!$res) {
                    throw new \Exception('状态变更失败');
                }
                //取消求购单表

                $res = Db('ppdd')->where(['id' => $oob['p_order_id'], 'zt' => 1])->update(['zt' => 3, 'update_time' => time(), 'cancel_time' => time()]);
                if (!$res) {
                    throw new \Exception('求购单状态修改失败');
                }
                //卖家冻结idc解冻
                $res = DB('user')->where(['id' => $oob['sell_id']])->setDec('idc_freeze', ($oob['num'] + $oob['fee']));
                if (!$res) {
                    throw new \Exception('卖家解冻币失败');
                }

            } else {
                //交易完成
                $res = Db('ppdd_jiaoyi')->where(['id' => $oob['id'], 'zt' => 5])->update(['zt' => 2, 'update_time' => time()]);
                if (!$res) {
                    throw new \Exception('状态变更失败');
                }
                $res = Db('ppdd')->where(['id' => $oob['p_order_id'], 'zt' => 1])->update(['zt' => 2, 'update_time' => time(), 'cancel_time' => time()]);
                if (!$res) {
                    throw new \Exception('求购单状态修改失败');
                }
                $sell_info = userinfobyid($oob['sell_id']);
                $dec_num = $oob['num'] + $oob['fee'];
                $res = DB('user')->where(['id' => $oob['sell_id']])->update(['idc_freeze' => $sell_info['idc_freeze'] - $dec_num, 'idc' => $sell_info['idc'] - $dec_num, 'update_time' => time()]);
                if (!$res) {
                    throw new \Exception('卖家扣币失败');
                }
                //
                $log_insert = [
                    'uid' => $oob['sell_id'],
                    'uname' => $oob['sell_user'],
                    'type' => 2,
                    'mqian' => $sell_info['idc'],
                    'money' => -$dec_num,
                    'mhou' => $sell_info['idc'] - $dec_num,
                    'note' => '卖家出售成功',
                    'order_id' => $oob['id'],
                    'order_sn' => $oob['g_order_sn'],
                    'time' => time(),
                ];
                $res = Db('logocoin')->insert($log_insert);
                if (!$res) {
                    throw new \Exception('币使用记录添加失败');
                }

                //买家账户增加idc
                $userinfo = userinfobyid($oob['buy_id']);
                $res = Db('user')->where(['id' => $oob['buy_id']])->setInc('idc', $oob['num']);
                if (!$res) {
                    throw new \Exception('买家增加币失败');
                }
                //
                $log_insert = [
                    'uid' => $oob['buy_id'],
                    'uname' => $oob['buy_user'],
                    'type' => 1,
                    'mqian' => $userinfo['idc'],
                    'money' => $oob['num'],
                    'mhou' => $userinfo['idc'] + $oob['num'],
                    'note' => '买家购买成功',
                    'order_id' => $oob['id'],
                    'order_sn' => $oob['g_order_sn'],
                    'time' => time(),
                ];
                $res = Db('logocoin')->insert($log_insert);
                if (!$res) {
                    throw new \Exception('币使用记录添加失败');
                }
            }
            //查询订单信息

            DB::commit();
            $is_success = 1;
            $msg = '处理成功';
            //$this->success('处理成功');
        } catch (\Exception $e) {
            DB::rollback();
            $this->error($e->getMessage());
        }
        if ($is_success) {
            $this->success($msg);
        }
    }

    /**
     * @name 投诉删除
     * @author zay
     * @Date 2020/3/19 0019 15:25
     */
    public function tsdel(Request $request)
    {
        $id = $request->param('id', 0);
        $oob = DB('tousu')->where(array('id' => $id, 'is_del' => 0))->find();
        if (empty($oob)) {
            $this->error('投诉已被删除，请勿重复操作');
        }
        $oob = DB('tousu')->where(array('id' => $id, 'is_del' => 0))->update(['is_del' => 1, 'deal_time' => time()]);
        if ($oob) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}